package com.dev.utils.app;

import com.dev.DevUtils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;

public final class BarUtils {

    private BarUtils() {
    }
    /**
     * 获取 StatusBar 高度
     * @return StatusBar 高度
     */
    public static int getStatusBarHeight() {
        return (int) (getRealHeight(DevUtils.getContext()) - getHeight(DevUtils.getContext()));
    }
    public static float getHeight(Context context) {
        Point point = new Point();
        DisplayManager.getInstance().getDefaultDisplay(context).get().getSize(point);
        return point.getPointY();
    }

    public static float getRealHeight(Context context) {
        Point point = new Point();
        DisplayManager.getInstance().getDefaultDisplay(context).get().getRealSize(point);
        return point.getPointY();
    }


    public static boolean setStatusBarVisibility(
            final Ability activity,
            final boolean isVisible
    ) {
        return setStatusBarVisibility(AbilityUtils.getWindow(activity), isVisible);
    }


    /**
     * 设置 StatusBar 是否显示
     * @param window    {@link Window}
     * @param isVisible 是否显示 StatusBar
     * @return {@code true} success, {@code false} fail
     */
    public static boolean setStatusBarVisibility(
            final Window window,
            final boolean isVisible
    ) {
        if (window != null) {
            if (isVisible) {

                showStatusBarView(window);
                addMarginTopEqualStatusBarHeight(new DirectionalLayout(DevUtils.getContext()));
            } else {
                hideStatusBarView(window);
                subtractMarginTopEqualStatusBarHeight(new DirectionalLayout(DevUtils.getContext()));
            }
            return true;
        }
        return false;
    }


    /**
     * 显示 StatusBar View
     * @param window {@link Window}
     */
    private static void showStatusBarView(final Window window) {
        WindowManager.getInstance().getTopWindow().get()
                .setStatusBarVisibility(Component.VISIBLE);
    }

    /**
     * 隐藏 StatusBar View
     * @param window {@link Window}
     */
    private static void hideStatusBarView(final Window window) {
        WindowManager.getInstance().getTopWindow().get()
                .setStatusBarVisibility(Component.INVISIBLE);
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.rgb(41, 121, 255));
    }


    /**
     * 移除 View 向上 StatusBar 同等高度边距
     * @param view {@link Component}
     * @return {@code true} success, {@code false} fail
     */
    public static boolean subtractMarginTopEqualStatusBarHeight(final Component view) {
        if (view != null ) {
            Object haveSetOffset = view.getTag();
            if (haveSetOffset == null || !(Boolean) haveSetOffset) return false;
            ComponentContainer.LayoutConfig layoutParams =  view.getLayoutConfig();
            layoutParams.setMargins(layoutParams.getMarginLeft(), layoutParams.getMarginTop() - getStatusBarHeight(),
                    layoutParams.getMarginRight(), layoutParams.getMarginBottom());
            view.setLayoutConfig(layoutParams);
            view.setTag(false);
            return true;
        }
        return false;
    }

    private static final String TAG_OFFSET     = "TAG_OFFSET";

    /**
     * 添加 View 向上 StatusBar 同等高度边距
     * @param view {@link Component}
     * @return {@code true} success, {@code false} fail
     */
    public static boolean addMarginTopEqualStatusBarHeight(final Component view) {
        if (view != null ) {
            Object haveSetOffset = view.getTag();
            if (haveSetOffset != null && (Boolean) haveSetOffset) return false;
            ComponentContainer.LayoutConfig layoutParams =  view.getLayoutConfig();
            layoutParams.setMargins(layoutParams.getMarginLeft(), layoutParams.getMarginTop() + getStatusBarHeight(),
                    layoutParams.getMarginRight(), layoutParams.getMarginBottom());
            view.setLayoutConfig(layoutParams);
            view.setTag(true);
            return true;
        }
        return false;
    }
}

